<template>
  <view>
    <view class="user_info">
      <view class="user_icon">
        <image mode="widthFix" :src="imgDetail.user.avatar"></image>
      </view>
      <view class="user_desc">
        <view class="user_name">{{ imgDetail.user.name }}</view>
        <view class="user_time">{{ imgDetail.cnTime }}</view>
      </view>
    </view>

    <view class="high_img">
      <swiper-action @swiperAction="handleSwiperAction">
        <image mode="widthFix" :src="imgDetail.thumb"></image>
      </swiper-action>
    </view>

    <view class="user_rank">
      <view class="rank">
        <text class="iconfont icondianzan">{{ imgDetail.rank }}</text>
      </view>
      <view class="user_collect">
        <text class="iconfont iconshoucang">收藏</text>
      </view>
    </view>

    <view class="comment hot" v-if="hot.length">
      <view class="comment_title">
        <text class="iconfont iconhot1"></text>
        <text class="comment_text">最热评论</text>
      </view>
      <view class="comment_list">
        <view class="comment_item" v-for="item in hot" :key="item.id">
          <view class="comment_user">
            <view class="user_icon">
              <image mode="widthFix" :src="item.user.avatar"></image>
            </view>
            <view class="user_name">
              <view class="user_nickname">{{ item.user.name }}</view>
              <view class="user_time">{{ item.cntime }}</view>
            </view>
            <view class="user_badge">
              <image
                v-for="item2 in item.user.title"
                :key="item2.icon"
                :src="item2.icon"
              ></image>
            </view>
          </view>
          <view class="comment_desc">
            <view class="comment_content">{{ item.content }}</view>
            <view class="comment_like">
              <text class="iconfont icondianzan">{{ item.size }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <view class="comment new" v-if="comment.length">
      <view class="comment_title">
        <text class="iconfont iconpinglun"></text>
        <text class="comment_text">最新评论</text>
      </view>
      <view class="comment_list">
        <view class="comment_item" v-for="item in comment" :key="item.id">
          <view class="comment_user">
            <view class="user_icon">
              <image mode="widthFix" :src="item.user.avatar"></image>
            </view>
            <view class="user_name">
              <view class="user_nickname">{{ item.user.name }}</view>
              <view class="user_time">{{ item.cntime }}</view>
            </view>
            <view class="user_badge">
              <image
                v-for="item2 in item.user.title"
                :key="item2.icon"
                :src="item2.icon"
              ></image>
            </view>
          </view>
          <view class="comment_desc">
            <view class="comment_content">{{ item.content }}</view>
            <view class="comment_like">
              <text class="iconfont icondianzan">{{ item.size }}</text>
            </view>
          </view>
        </view>
      </view>
    </view>

    <view class="download">
      <view class="download_btn" @click="handleDownload">下载图片</view>
    </view>

  </view>
</template>

<script>
import moment from 'moment'
moment.locale('zh-cn')
import swiperAction from '@/components/swiperAction'
export default {
  components: {
    swiperAction
  },
  data() {
    return {
      imgDetail: {},
      hot: [],
      comment: [],
      imgIndex: 0
    }
  },
  onLoad() {
    console.log(getApp().globalData)
    const { imgIndex } = getApp().globalData

    this.imgIndex = imgIndex
    this.getData()
  },
  methods: {
    getData() {
      const { imgList } = getApp().globalData
      this.imgDetail = imgList[this.imgIndex]

      this.imgDetail.cnTime = moment(this.imgDetail.atime * 1000).fromNow()

      this.getComments(this.imgDetail.id)
    },
    getComments(id) {
      this.request({
        url: `image/v2/wallpaper/wallpaper/${id}/comment`
      }).then(res => {
        console.log(res)
        res.res.hot.forEach(v => (v.cntime = moment(v.atime * 1000).fromNow()))
        this.hot = res.res.hot
        res.res.comment.forEach(
          v => (v.cntime = moment(v.atime * 1000).fromNow())
        )
        this.comment = res.res.comment
      })
    },
    handleSwiperAction(e) {
      const { imgList } = getApp().globalData
      if (e.direction === 'left' && this.imgIndex < imgList.length - 1) {
        this.imgIndex++
        this.getData()
      }else if(e.direction === 'right' && this.imgIndex > 0) {
        this.imgIndex--
        this.getData()
      }else {
        uni.showToast({
          title: '没有数据了',
          icon: 'none'
        })
      }
    },
    async handleDownload() {
      await uni.showLoading({title: '下载中'})

      const result1 = await uni.downloadFile({
        url: this.imgDetail.img
      })
      if(result1[0]) {
        uni.showToast({
          title: '获取资源失败',
          icon: 'none'
        })
        return
      }
      console.log(result1)
      const {tempFilePath} = result1[1]
      const result2 = await uni.saveImageToPhotosAlbum({
        filePath: tempFilePath
      })
      console.log(result2)
      uni.hideLoading()
      
      if(result2[0] && result2[0].errMsg === 'saveImageToPhotosAlbum:fail cancel') {
        uni.showToast({
          title: '下载取消',
          icon: 'none'
        })
        return
      }
      await uni.showToast({
        title: '下载成功'
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.user_info {
  display: flex;
  padding: 20rpx;
  .user_icon {
    padding: 0 20rpx;
    image {
      width: 88rpx;
      height: auto;
      border-radius: 50%;
    }
  }
  .user_desc {
    .user_name {
      color: #000;
      font-weight: 600;
    }

    .user_time {
      color: #aaa;
      font-size: 24rpx;
      padding: 6rpx 0;
    }
  }
}

.high_img {
  border-bottom: 2rpx solid #ccc;
}

.user_rank {
  display: flex;
  height: 80rpx;
  border-bottom: 5rpx solid #eee;
  .rank {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    .iconfont {
    }
  }

  .user_collect {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    .iconfont {
    }
  }
}

.comment {
  .comment_title {
    padding: 15rpx;
    .iconfont {
      color: red;
      font-size: 40rpx;
    }

    .comment_text {
      font-weight: 600;
      font-size: 28rpx;
      color: #666;
      margin-left: 10rpx;
    }
  }

  .comment_list {
    .comment_item {
      border-bottom: 15rpx solid #eee;
      .comment_user {
        display: flex;
        padding: 20rpx 0;
        .user_icon {
          width: 15%;
          display: flex;
          justify-content: center;
          align-items: center;
          image {
            width: 80%;
          }
        }

        .user_name {
          flex: 1;
          .user_nickname {
            color: #777;
          }

          .user_time {
            color: #aaa;
            font-size: 24rpx;
            padding: 5rpx 0;
          }
        }

        .user_badge {
          margin-right: 10rpx;
          image {
            width: 40rpx;
            height: 40rpx;
            display: inline-block;
            margin: 0 10rpx;
          }
        }
      }

      .comment_desc {
        display: flex;
        padding: 30rpx 0;
        .comment_content {
          flex: 1;
          padding-left: 15%;
          color: #000;
        }

        .comment_like {
          text-align: right;
          padding-right: 15rpx;
          .iconfont {
          }
        }
      }
    }
  }
}

.new {
  .iconpinglun {
    color: aqua !important;
  }
}

.download {
  height: 120rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  .download_btn {
    width: 90%;
    height: 80%;
    background-color: $color;
    color: #fff;
    font-size: 50rpx;
    font-weight: 600;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
